// Ionic Starter App
var domain;
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
define([
  'services/services',
  'route'
],
 function(){
    'use strict';
    var app = angular.module('starter', ['ionic',
      'starter.controllers',
      //'starter.login',
      //'starter.register',
      //'starter.reset-password',
      //'starter.create-new-password',
      //
      //
      //
      //'starter.tabSearch',
      //
      //
      //
      //'starter.tabCart',
      //'starter.firstCheckout',
      //'starter.secondCheckout',
      //'starter.addressList',
      //'starter.addAddress',
      //'starter.orderConfirmed',
      //
      //
      //'starter.tabMe',
      //'starter.myAccount',
      //'starter.aboutUs',
      //'starter.helpfulQuestions',
      //'starter.privaryPolicy',
      //'starter.myOrders',
      //'starter.orderDetails',


      'starter.services'])

    app.run(function($ionicPlatform,$rootScope,$location,$ionicHistory) {
      $ionicPlatform.ready(function() {
        localStorage.setItem("device",device.platform);
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
          cordova.plugins.Keyboard.disableScroll(true);

        }
        if (window.StatusBar) {
          // org.apache.cordova.statusbar required
          StatusBar.styleDefault();
        }
      });


        // 开机前提前禁掉tab栏位
        $rootScope.$on('$ionicView.beforeEnter', function(event, view) {
          $rootScope.isHideTabBar = (

            view.stateName === 'tab.login'
            ||view.stateName === 'tab.register'
            ||view.stateName === 'tab.reset-password'
            ||view.stateName === 'tab.create-new-password'


            ||view.stateName === 'tab.first-checkout'
            ||view.stateName === 'tab.second-checkout'
            ||view.stateName === 'tab.address-list'
            ||view.stateName === 'tab.add-address'

            ||view.stateName === 'tab.about-us'
            ||view.stateName === 'tab.my-account'
            ||view.stateName === 'tab.privary-policy'
            ||view.stateName === 'tab.helpful-questions'





          );
          console.log('Before Enter..', view.stateName);
        });

        //双击退出
        $ionicPlatform.registerBackButtonAction(function(e) {
          //判断处于哪个页面时双击退出
          if ($location.path() == '/tab/main' ||$location.path() == '/tab/account') {
            if ($rootScope.backButtonPressedOnceToExit) {
              ionic.Platform.exitApp();
            } else {
              $rootScope.backButtonPressedOnceToExit = true;
              $cordovaToast.showShortBottom('再按一次退出系统');
              setTimeout(function() {
                $rootScope.backButtonPressedOnceToExit = false;
              }, 2000);
            }
          }else if($location.path() == '/tab/main_login'){
            $ionicHistory.goBack();
            $scope.$apply();
          }
          else if ($ionicHistory.backView()) {
            $ionicHistory.goBack();
            $scope.$apply();
          } else {
            $ionicHistory.goBack();
            $scope.$apply();
          }
          e.preventDefault();
          return false;
        }, 101);
    })



    app.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {
        $ionicConfigProvider.platform.android.navBar.alignTitle('center');
        $ionicConfigProvider.platform.android.tabs.style('standard');
        $ionicConfigProvider.platform.android.tabs.position('standard');
        $ionicConfigProvider.backButton.previousTitleText(false);
        $ionicConfigProvider.backButton.text('');

      // Ionic uses AngularUI Router which uses the concept of states
      // Learn more here: https://github.com/angular-ui/ui-router
      // Set up the various states which the app can be in.
      // Each state's controller can be found in controllers.js
      $stateProvider

      // setup an abstract state for the tabs directive
        .state('tab', {
        url: '/tab',
        abstract: true,
        templateUrl: 'templates/tabs.html'
      })

      // Each tab has its own nav history stack:


      //  my home page class
      .state('tab.home', {
        url: '/home',
        views: {
          'tab-home': {
            templateUrl: 'templates/tab-home.html',
            controller: 'HomeCtrl'
          }
        }
      })

        .state('tab.login', {
          url: '/login',
          views: {
            'tab-home': {
              templateUrl: 'templates/main/login.html',
              controller: 'LoginCtrl'
            }
          }
        })

        .state('tab.register', {
          url: '/register',
          views: {
            'tab-home': {
              templateUrl: 'templates/main/register.html',
              controller: 'RegisterCtrl'
            }
          }
        })
        .state('tab.reset-password', {
          url: '/reset-password',
          views: {
            'tab-home': {
              templateUrl: 'templates/main/reset-password.html',
              controller: 'ResetPasswordCtrl'
            }
          }
        })
        .state('tab.create-new-password', {
          url: '/create-new-password',
          views: {
            'tab-home': {
              templateUrl: 'templates/main/create-new-password.html',
              controller: 'CreateNewPasswordCtrl'
            }
          }
        })


      //  my search page class
      .state('tab.search', {
          url: '/search',
          views: {
            'tab-search': {
              templateUrl: 'templates/tab-search.html',
              controller: 'SearchCtrl'
            }
          }
        })


      //  my cart page class
      .state('tab.cart', {
          url: '/cart',
          views: {
            'tab-cart': {
              templateUrl: 'templates/tab-cart.html',
              controller: 'CartCtrl'
            }
          }
        })

        .state('tab.first-checkout', {
          url: '/first-checkout',
          views: {
            'tab-cart': {
              templateUrl: 'templates/cart/first-checkout.html',
              controller: 'FirstCheckoutCtrl'
            }
          }
        })
        .state('tab.second-checkout', {
          url: '/second-checkout',
          views: {
            'tab-cart': {
              templateUrl: 'templates/cart/second-checkout.html',
              controller: 'SecondCheckoutCtrl'
            }
          }
        })
        .state('tab.address-list', {
          url: '/address-list',
          views: {
            'tab-cart': {
              templateUrl: 'templates/cart/address-list.html',
              controller: 'AddressListCtrl'
            }
          }
        })
        .state('tab.add-address', {
          url: '/add-address',
          views: {
            'tab-cart': {
              templateUrl: 'templates/cart/add-address.html',
              controller: 'AddAddressCtrl'
            }
          }
        })

        .state('tab.order-confirmed', {
          url: '/order-confirmed',
          views: {
            'tab-cart': {
              templateUrl: 'templates/cart/order-confirmed.html',
              controller: 'OrderConfirmedCtrl'
            }
          }
        })




    //  my me page class
      .state('tab.me', {
        url: '/me',
        views: {
          'tab-me': {
            templateUrl: 'templates/tab-me.html',
            controller: 'MeCtrl'
          }
        }
      })

        .state('tab.my-account', {
          url: '/my-account',
          views: {
            'tab-me': {
              templateUrl: 'templates/me/my-account.html',
              controller: 'MyAccountCtrl'
            }
          }
        })

        .state('tab.my-orders', {
          url: '/my-orders',
          views: {
            'tab-me': {
              templateUrl: 'templates/me/my-orders.html',
              controller: 'MyOrdersCtrl'
            }
          }
        })

        .state('tab.about-us', {
          url: '/about-us',
          views: {
            'tab-me': {
              templateUrl: 'templates/me/about-us.html',
              controller: 'AboutUsCtrl'
            }
          }
        })

        .state('tab.privary-policy', {
          url: '/privary-policy',
          views: {
            'tab-me': {
              templateUrl: 'templates/me/privary-policy.html',
              controller: 'PrivaryPolicyCtrl'
            }
          }
        })

        .state('tab.order-details', {
          url: '/order-details',
          views: {
            'tab-me': {
              templateUrl: 'templates/me/order-details.html',
              controller: 'OrderDetailsCtrl'
            }
          }
        })
      ;

      // if none of the above states are matched, use this as the fallback
      $urlRouterProvider.otherwise('/tab/home');

    });




    app.directive("shadediv", function() {
      return {
        restrict : "AE",
        template :
                  "<div style='width: 100%;background: #999999;height: {{shadeDivHeight}};opacity: 0.5;position: fixed'>"+
                  "</div>"
      };
    });
   return app;
 })
